// pages/presentAccount/presentAccount.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bankList: [],
    page_count:1,
    page:1,
    row:99, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var that = this;
    // that.bankList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.bankList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  reachBottom: function () {
    // var that = this;
    // var page = that.data.page + 1;
    // if (page > that.data.page_count) {
    //   that.setData({
    //     noMore: true
    //   })
    //   return false;
    // }
    // that.setData({
    //   page: page
    // })
    // that.bankList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 删除
  delcard: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var bank_id = e.currentTarget.dataset.bank_id;
    wx.showModal({
      title: '提示',
      content: '是否删除该账户',
      success: function (res) {
        if (res.confirm) {          
          app.ajax({
            url: 'Salesman/Account/delBank',
            method: "POST",
            data: {
              bank_id: bank_id
            },
            success: function (res) {
              var flag = res.data;
              app.showToast(flag.message);
              if (flag.code > 0) {
                var bankList = that.data.bankList
                bankList.splice(id, 1);
                that.setData({
                  bankList: bankList
                });
                return false;
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 编辑
  eidtcard: function (e) {
    var bank_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../addeditAccount/addeditAccount?type=1&&bank_id=' + bank_id,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  // 添加账户
  addAccount: function () {
    wx.navigateTo({
      url: '../addeditAccount/addeditAccount?type=2',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  
  bankList(){
    var that = this;
    app.ajax({
      url: 'Salesman/Account/bankList',
      method: "POST",
      data: {
        page:that.data.page,
        row:that.data.row
      },
      success: function (res) {
        var flag = res.data;
        if (flag.code < 0) {
          app.showToast(flag.message);
          return false;
        }
        // var bankList = that.data.bankList.concat(flag.data.data);
        that.setData({
          bankList: flag.data.data,
          // page_count: flag.data.page.total_page
        });
      }
    })
  }
})